package net.scrumtool.controller;

import java.util.List;

import net.scrumtool.model.Collaborator;
import net.scrumtool.model.Participate;
import net.scrumtool.model.ParticipateId;
import net.scrumtool.model.Role;
import net.scrumtool.model.Sprint;
import net.scrumtool.technics.DAO;
import net.scrumtool.technics.LabelProviderEditAddCollaboratorsSprint;
import net.scrumtool.technics.LabelProviderEditRemoveCollaboratorsSprint;
import net.scrumtool.technics.LabelProviderEditRoleCollaborators;
import net.scrumtool.technics.Utils;
import net.scrumtool.view.EditCollaboratorsSprint;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;

public class CeditCollaboratorsSprint {

	EditCollaboratorsSprint view;
	private Participate participateActif;
	private Role roleEditActif;
	private Collaborator collaboratorActif;
	private Role roleActif;

	public CeditCollaboratorsSprint(EditCollaboratorsSprint view) {
		this.view = view;
	}

	public void init(Sprint sprint) {

		// Bouton Edit Collaborators (Add/Remove)
		view.getBtnEditCollaborators().setText("Remove");
		view.getBtnEditCollaborators().setImage(Utils.getImage(Utils.IMG_DEL));
		view.getBtnEditCollaborators().setEnabled(false);

		// SashFormRemoveCollaborator
		view.getSashFormRemoveCollaborators().setWeights(new int[] { 1, 0 });

		// Bouton Edit Role
		view.getBtnEditRole().setEnabled(false);

		// Bouton Save (Edit Role)
		view.getBtnSaveEditRole().setEnabled(false);

		view.getTableViewerRemoveCollaborators().setContentProvider(new ArrayContentProvider());
		view.getTableViewerRemoveCollaborators().setLabelProvider(new LabelProviderEditRemoveCollaboratorsSprint());
		view.getTableViewerRemoveCollaborators().setInput(DAO.getParticipateBySprint(sprint));

		view.getTableViewerAddCollaborators().setContentProvider(new ArrayContentProvider());
		view.getTableViewerAddCollaborators().setLabelProvider(new LabelProviderEditAddCollaboratorsSprint());
		view.getTableViewerAddCollaborators().setInput(DAO.getCollaboratorsNotInSprint(sprint));

		view.getTableViewerRole().setContentProvider(new ArrayContentProvider());
		view.getTableViewerRole().setLabelProvider(new LabelProviderEditRoleCollaborators());
		view.getTableViewerRole().setInput(DAO.getRole());
	}

	public void listener(final Sprint sprint) {

		// tabFolder
		view.getTabFolder().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				if (view.getTabFolder().getSelection()[0].getText() == "Collaborator(s) in sprint") {
					view.getBtnEditRole().setEnabled(false);
					view.getBtnEditCollaborators().setEnabled(false);
					view.getBtnEditCollaborators().setText("Remove");
					view.getBtnEditCollaborators().setImage(Utils.getImage(Utils.IMG_DEL));
				} else {
					view.getTableRole().setEnabled(false);
					view.getSashFormRemoveCollaborators().setWeights(new int[] { 1, 0 });
					view.getBtnEditRole().setEnabled(false);
					view.getBtnEditCollaborators().setEnabled(false);
					view.getBtnEditCollaborators().setText("Add");
					view.getBtnEditCollaborators().setImage(Utils.getImage(Utils.IMG_ADD));
				}

			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		// Table role
		view.getTableRole().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				StructuredSelection sel = (StructuredSelection) view.getTableViewerRole().getSelection();
				setRoleActif((Role) sel.getFirstElement());

				view.getBtnEditCollaborators().setEnabled(true);

			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		// Table add collaborators
		view.getTableAddCollaborators().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				StructuredSelection sel = (StructuredSelection) view.getTableViewerAddCollaborators().getSelection();
				setCollaboratorActif((Collaborator) sel.getFirstElement());

				view.getTableRole().setEnabled(true);

			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		// Table remove collaborators
		view.getTableRemoveCollaborators().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				StructuredSelection sel = (StructuredSelection) view.getTableViewerRemoveCollaborators().getSelection();
				setParticipateActif((Participate) sel.getFirstElement());

				view.getBtnEditCollaborators().setEnabled(true);
				view.getBtnEditRole().setEnabled(true);
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		// Bouton Edit Collaborators (Add/Remove)
		view.getBtnEditCollaborators().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				if (view.getBtnEditCollaborators().getText() == "Remove") {
					Collaborator col = new Collaborator(participateActif.getCollaborator().getId(), participateActif.getCollaborator().getLogin(), participateActif.getCollaborator().getPassword(), participateActif.getCollaborator().getFirstname(), participateActif.getCollaborator().getLastname(), participateActif.getCollaborator().getEmail(), participateActif.getCollaborator().getAdministrator(), participateActif.getCollaborator().getUserstories(), participateActif.getCollaborator().getParticipates(), participateActif.getCollaborator().getPlayroles(), participateActif.getCollaborator().getComments());

					DAO.delParticipate(participateActif);

					view.getTableViewerRemoveCollaborators().remove(participateActif);
					view.getTableViewerAddCollaborators().add(col);

					view.getSashFormRemoveCollaborators().setWeights(new int[] { 1, 0 });
					view.getBtnEditCollaborators().setEnabled(false);
					view.getBtnEditRole().setEnabled(false);

					Utils.MsgBox(view.getShell(), "Collaborator delete from Sprint", "information");
				} else {
					ParticipateId id = new ParticipateId(sprint.getId(), collaboratorActif.getId(), roleActif.getId());
					Participate pr = new Participate(id, sprint, roleActif, collaboratorActif);

					DAO.addParticipate(pr);

					view.getTableViewerAddCollaborators().remove(collaboratorActif);
					view.getTableViewerRemoveCollaborators().add(pr);

					view.getBtnEditCollaborators().setEnabled(false);
					view.getTableRole().setEnabled(false);

					Utils.MsgBox(view.getShell(), "Collaborator add successfully", "information");
				}

			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		// Bouton Edit Role
		view.getBtnEditRole().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				view.getSashFormRemoveCollaborators().setWeights(new int[] { 277, 138 });
				view.getBtnSaveEditRole().setEnabled(false);
				view.getLblNameCollaboratorEditRole().setText(participateActif.getCollaborator().getFirstname() + " " + participateActif.getCollaborator().getLastname());

				List<Role> role = DAO.getRoleWhereNameIsNot(participateActif.getRole().getName());
				view.getComboViewer().setContentProvider(new ArrayContentProvider());
				view.getComboViewer().setInput(role);
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		// Bouton Save (Edit Role)
		view.getBtnSaveEditRole().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {

				ParticipateId id = new ParticipateId(sprint.getId(), participateActif.getCollaborator().getId(), roleEditActif.getId());
				Participate pr = new Participate(id, sprint, roleEditActif, participateActif.getCollaborator());

				DAO.delParticipate(participateActif);
				DAO.addParticipate(pr);

				view.getTableViewerRemoveCollaborators().remove(participateActif);
				view.getTableViewerRemoveCollaborators().add(pr);

				view.getSashFormRemoveCollaborators().setWeights(new int[] { 1, 0 });
				view.getBtnEditRole().setEnabled(false);
				view.getBtnSaveEditRole().setEnabled(false);
				view.getBtnEditCollaborators().setEnabled(false);

				Utils.MsgBox(view.getShell(), "Role edit successfully", "information");
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		// Combo Role
		view.getComboViewer().addSelectionChangedListener(new ISelectionChangedListener() {

			@Override
			public void selectionChanged(SelectionChangedEvent arg0) {
				IStructuredSelection sel = (IStructuredSelection) (view.getComboViewer().getSelection());
				Role role = (Role) (sel.iterator().next());
				setRoleEditActif(role);

				view.getBtnSaveEditRole().setEnabled(true);

			}
		});

	}

	public void setParticipateActif(Participate participateActif) {
		this.participateActif = participateActif;
	}

	public void setRoleEditActif(Role roleEditActif) {
		this.roleEditActif = roleEditActif;
	}

	public void setCollaboratorActif(Collaborator collaboratorActif) {
		this.collaboratorActif = collaboratorActif;
	}

	public void setRoleActif(Role roleActif) {
		this.roleActif = roleActif;
	}
}
